package org.example.his.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.example.his.common.api.CommonResponse;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;

public class RequestToJsonObject {

    /**
     * httpRequest转换为JSONObject
     * @param request
     * @return JSONObject
     */
    public static JSONObject RequestToJsonObject(HttpServletRequest request){
        String requestBody="";
        JSONObject jsonObject = new JSONObject();
        try {
            BufferedReader reader = request.getReader();
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            requestBody = sb.toString();
            /**
             * requestBody是形如
             * {"orderId":"","username":"","orderDate":"","shipAddress1":"","shipAddress2":"","shipCity":"","shipState":"","shipZip":"","shipCountry":"","billAddress1":"","billAddress2":"","billCity":"","billZip":"","billCountry":"","billstate":"","courier":"","totalPrice":"","billToFirstName":"","billToLastName":"","shipToFirstName":"","shipToLastName":"","creditCard":"","expiryDate":"","cardType":"","locale":"","status":"","state":"","sendOut":""}
             * [Orders(orderId=8888, username=ch, orderDate=null, shipAddress1=china, shipAddress2=hunan, shipCity=changsha, shipState=null, shipZip=null, shipCountry=null, billAddress1=null, billAddress2=Long, billCity=null, billZip=null, billCountry=null, billstate=null, courier=null, totalPrice=null, billToFirstName=null, billToLastName=Chen, shipToFirstName=null, shipToLastName=null, creditCard=null, expiryDate=null, cardType=null, locale=null, status=null, state=null, sendOut=null, lineItems=[])]
             * 的数据
             */
            System.out.println(requestBody);
            jsonObject = JSON.parseObject(requestBody);
            return jsonObject;
        }catch(IOException e){
                // 处理异常
        }
        return jsonObject;
    }
}
